package ooday03;

public class PolymorphicDemo {

    public static void main(String[] args) {
        Dog o1 = new Dog("大黄", 2, "黄");
        System.out.println(o1.name + " " + o1.age + " " + o1.color);
        o1.eat();
        o1.drink();
        o1.swim();
        o1.lookHome();

        Animal o2 = new Dog("小白", 1, "白");
        System.out.println(o2.name + " " + o2.age + " " + o2.color);
        o2.eat();
        o2.drink();

        Swim o3 = new Dog("小黑", 8, "黑");
        o3.swim();
        System.out.println("-------------------------------------");

        //向上造型的应用1
        Dog dog = new Dog("大黄", 2, "黄");
        Fish fish = new Fish("泡泡", 4, "红");
        Chick chick = new Chick("太美", 5, "棕");

        Master master = new Master();
        master.feed(dog);
        master.feed(fish);
        master.feed(chick);
        System.out.println("-------------------------------------");

        //向上造型的应用2
        Animal[] animals = new Animal[5];
        animals[0] = new Dog("大黄", 2, "黄");
        animals[1] = new Fish("泡泡", 4, "红");
        animals[2] = new Chick("太美", 5, "棕");
        animals[3] = new Dog("小黑", 8, "黑");
        animals[4] = new Dog("小白", 1, "白");

        for (int i = 0; i < animals.length; i++) {
            System.out.println(animals[i].name);
            animals[i].eat();
            animals[i].drink();
            if (animals[i] instanceof Dog) {
                Dog d = (Dog) animals[i];
                d.lookHome();
            }
            if (animals[i] instanceof Chick) {
                Chick c = (Chick) animals[i];
                c.layEggs();
            }
            if (animals[i] instanceof Swim) {
                Swim s = (Swim) animals[i];
                s.swim();
            }
        }
        System.out.println("-------------------------------------");

        //向下转型，强转成功条件
        Animal o = new Dog("大黄", 2, "黄");
        Dog g = (Dog) o;
        Swim s = (Swim) o;
        System.out.println(o instanceof Dog);//true
        System.out.println(o instanceof Swim);//true
        System.out.println(o instanceof Fish);//false

        if (o instanceof Fish) {//false
            Fish f = (Fish) o;
        } else {
            System.out.println("o不是Fish类型");
        }


    }
}
